package a.netty.server;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;

@ChannelHandler.Sharable
public class HttpServerChannelHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

    @Override
    protected void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request) {
        System.out.println("Received HTTP Request");
        System.out.println("远程地址：" + ctx.channel().remoteAddress());
        System.out.println("server - HttpServerChannelHandler.messageReceived");
        System.out.println(request);
        System.out.println("uri:" + request.uri());
        System.out.print(request.content().toString(CharsetUtil.UTF_8));
//        1. ApiPost
//        HttpObjectAggregator$AggregatedFullHttpRequest(decodeResult: success, version: HTTP/1.1, content: CompositeByteBuf(ridx: 0, widx: 23, cap: 23, components=1))
//        POST /path?param1=234&param2=45 HTTP/1.1
//        User-Agent: PostmanRuntime-ApipostRuntime/1.1.0
//        Cache-Control: no-cache
//        content-type: application/json
//        Accept: */*
//        Accept-Encoding: gzip, deflate, br
//        Connection: keep-alive
//        cookie: fileDownload=true
//        Host: localhost:8080
//        Content-Length: 23

//        uri:/path?param1=234&param2=45

//        {
//            "name": "123"
//        }



//        2. 浏览器
//        HttpObjectAggregator$AggregatedFullHttpRequest(decodeResult: success, version: HTTP/1.1, content: CompositeByteBuf(ridx: 0, widx: 0, cap: 0, components=0))
//        GET / HTTP/1.1
//        Host: localhost:8080
//        Connection: keep-alive
//        sec-ch-ua: "Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"
//        sec-ch-ua-mobile: ?0
//        sec-ch-ua-platform: "Windows"
//        Upgrade-Insecure-Requests: 1
//        User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
//        Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
//        Sec-Fetch-Site: none
//        Sec-Fetch-Mode: navigate
//        Sec-Fetch-User: ?1
//        Sec-Fetch-Dest: document
//        Accept-Encoding: gzip, deflate, br, zstd
//        Accept-Language: zh-CN,zh;q=0.9
//        Cookie: MUIDB=3A184D88C9536EF831BF58A2C8106F27
//        content-length: 0
//        uri:/

//        HttpObjectAggregator$AggregatedFullHttpRequest(decodeResult: success, version: HTTP/1.1, content: CompositeByteBuf(ridx: 0, widx: 0, cap: 0, components=0))
//        GET /favicon.ico HTTP/1.1
//        Host: localhost:8080
//        Connection: keep-alive
//        sec-ch-ua-platform: "Windows"
//        User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
//        sec-ch-ua: "Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"
//        sec-ch-ua-mobile: ?0
//        Accept: image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8
//        Sec-Fetch-Site: same-origin
//        Sec-Fetch-Mode: no-cors
//        Sec-Fetch-Dest: image
//        Referer: http://localhost:8080/
//        Accept-Encoding: gzip, deflate, br, zstd
//        Accept-Language: zh-CN,zh;q=0.9
//        Cookie: MUIDB=3A184D88C9536EF831BF58A2C8106F27
//        content-length: 0
//        uri:/favicon.ico

        // 拿到参数和body 调用controller获取结果 序列化 返回
        ByteBuf byteBuf = Unpooled.copiedBuffer("{\"code\": 200, \"msg\": \"OK!\"}", CharsetUtil.UTF_8);
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, byteBuf);
        response.headers().add(HttpHeaderNames.CONTENT_TYPE, "application/json");
        response.headers().add(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(byteBuf.readableBytes()));

        ctx.writeAndFlush(response);
    }
}
